package com.eascs.cfsapi.service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.cfsapi.dao.CfsApiLoanapplyDao;
import com.eascs.cfsapi.vo.LoanapplyPageVo;
import com.eascs.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CfsApiLoanapplyService {

    @Autowired
    CfsApiLoanapplyDao cfsApiLoanapplyDao;

    public PageResult<Object[]> queryLoanapplyPage(LoanapplyPageVo loanapplyPageVo){
        SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, loanapplyPageVo.getPageSize(), loanapplyPageVo.getPageIndex());
        StringBuffer stringBuffer=new StringBuffer("SELECT t.create_time,t.`name` as username,u.`name` as phone,t.loan_amount,t.loan_term," +
                "t.province,t.city,t.county,t.address FROM cf_loanapply t LEFT JOIN cf_user u ON t.create_id=u.id  where 1=1 ");
        if (StringUtil.isNotBlank(loanapplyPageVo.getName())){
            stringBuffer.append(" and t.name like ? ");
            sqlPageQuery.addLikeParam(loanapplyPageVo.getName());
        }
        if (StringUtil.isNotBlank(loanapplyPageVo.getProvince())){
            stringBuffer.append(" and t.province like ? ");
            sqlPageQuery.addLikeParam(loanapplyPageVo.getProvince());
        }
        if (StringUtil.isNotBlank(loanapplyPageVo.getCity())){
            stringBuffer.append(" and t.city like ? ");
            sqlPageQuery.addLikeParam(loanapplyPageVo.getCity());
        }
        if (StringUtil.isNotBlank(loanapplyPageVo.getStartTime())){
            stringBuffer.append(" and t.create_time >=? ");
            sqlPageQuery.addParam(loanapplyPageVo.getStartTime());
        }
        if (StringUtil.isNotBlank(loanapplyPageVo.getEndTime())){
            stringBuffer.append(" and t.create_time <=? ");
            sqlPageQuery.addParam(loanapplyPageVo.getEndTime());
        }
        stringBuffer.append(" order by t.create_time  desc ");
        sqlPageQuery.setQuerySql(stringBuffer.toString());
        return cfsApiLoanapplyDao.findDataWithPage(sqlPageQuery);
    }
}
